package util;

import account.entity.AccountVO;
import label.entity.LabelVO;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;


public class ObtainOther {

    private static InputStream inputStream = new TestMain().getClass().getResourceAsStream("/message.properties");
    private static Logger logger = LoggerFactory.getLogger(ObtainOther.class);

    private static Properties prop = new Properties();
    private final static String path = new TestMain().getClass().getResource("/message.properties").getPath();


    /**
     * 获取当前登录的账号
     *
     * @return
     */
    public static AccountVO obtainAccount() {
        HttpSession session = ServletActionContext.getRequest().getSession();
        return (AccountVO) session.getAttribute("accountVO");
    }

    /**
     * 获取当前登录账号的id
     *
     * @return
     */
    public static Integer obtainId() {
        return obtainAccount().getId();
    }

    /**
     * 获取存储在配置文件中对应的语句
     *
     * @param key
     * @return
     */
    public static String obtainMessage(String key) {
        String message = util.message.getInstance().get(key).toString();
        return message;
    }

    /**
     * 修改配置文件中对应的pageSize
     *
     * @param key
     * @param pageSize
     */
    public static void ChangAndSaveSize(String key, Integer pageSize) {
        FileOutputStream fos = null;
        try {
            prop.load(inputStream);
            prop.setProperty(key, pageSize.toString());
            fos = new FileOutputStream("D:\\Program Files\\Java\\ajax\\blog\\src\\main\\resources\\message.properties");

            prop.store(fos, null);

        } catch (FileNotFoundException e) {
            logger.error("FileNotFound error:\n", e);
        } catch (IOException e) {
            logger.error("error:\n", e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 判断页面传来的pageSize的参数是否与配置文件的相同
     * 若不同，则将页面传来的pageSize参数存入配置文件中
     *
     * @param key
     * @param pageSize
     * @return
     */
    public static Integer obtainPageSize(String key, Integer pageSize) {
        Integer pageSizeProperties = Integer.valueOf(obtainMessage(key));
        if (pageSize != null) {
            if (pageSize != pageSizeProperties) {
                ChangAndSaveSize(key, pageSize);
            }
            return pageSize;
        } else {
            return pageSizeProperties;
        }

    }

    /**
     * 去除重复的标签元素
     * @param labelVOs
     * @return
     */
    public static List<LabelVO> obtainLabels(List<LabelVO> labelVOs) {
        for (int i = 0; i < labelVOs.size(); i++) {
            String labelVOI = labelVOs.get(i).toString();
            for (int j = i+1; j < labelVOs.size(); j++) {
                String labelVOJ = labelVOs.get(j).toString();
                if ( labelVOI.equals(labelVOJ)){
                    labelVOs.remove(j);
                }

            }
        }
        return labelVOs;
    }
}


